package br.com.binding.rebind.template;

import br.com.binding.rebind.ListenerTemplate;
import br.com.binding.rebind.BoundModelClass;
import java.util.Map;
import java.util.List;
import com.google.gwt.core.ext.typeinfo.JField;

public class Bound_ViewTemplate
{
  protected static String nl;
  public static synchronized Bound_ViewTemplate create(String lineSeparator)
  {
    nl = lineSeparator;
    Bound_ViewTemplate result = new Bound_ViewTemplate();
    nl = null;
    return result;
  }

  protected final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl; //$NON-NLS-1$
  protected final String TEXT_1 = " "; //$NON-NLS-1$
  protected final String TEXT_2 = NL;
  protected final String TEXT_3 = "\tpublic Bound_"; //$NON-NLS-1$
  protected final String TEXT_4 = "_View() {" + NL + "\t\tsuper();" + NL + "\t\tinitBinding(); " + NL + "\t}" + NL + "\tprivate void initBinding() {" + NL; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
  protected final String TEXT_5 = "\t\t\t\t\t//"; //$NON-NLS-1$
  protected final String TEXT_6 = NL;
  protected final String TEXT_7 = "\t\t((SourcesPropertyChanges)"; //$NON-NLS-1$
  protected final String TEXT_8 = ")" + NL; //$NON-NLS-1$
  protected final String TEXT_9 = "\t\t\t"; //$NON-NLS-1$
  protected final String TEXT_10 = NL;
  protected final String TEXT_11 = "\t\t\t."; //$NON-NLS-1$
  protected final String TEXT_12 = NL;
  protected final String TEXT_13 = "\t}" + NL + "}" + NL; //$NON-NLS-1$ //$NON-NLS-2$

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append(TEXT_1);
    stringBuffer.append(TEXT_2);

	BoundModelClass modelClass = (BoundModelClass)argument;
	Map<JField, List<ListenerTemplate>> listeners = modelClass.getListenersMap();

    stringBuffer.append(TEXT_3);
    stringBuffer.append(modelClass.getClassName());
    stringBuffer.append(TEXT_4);

			for(JField field : listeners.keySet()) {
				for(ListenerTemplate template : listeners.get(field)){
    stringBuffer.append(TEXT_5);
    stringBuffer.append(field.getType().getSimpleSourceName());
    stringBuffer.append(TEXT_6);
if(field.getType().isClassOrInterface().isAssignableTo(modelClass.getModelType())){
			
    stringBuffer.append(TEXT_7);
    stringBuffer.append(field.getName());
    stringBuffer.append(TEXT_8);
}
			else {
    stringBuffer.append(TEXT_9);
    stringBuffer.append(field.getName());
    stringBuffer.append(TEXT_10);
}
    stringBuffer.append(TEXT_11);
    stringBuffer.append(template.generate(field));
    stringBuffer.append(TEXT_12);

				}
			}
		
    stringBuffer.append(TEXT_13);
    return stringBuffer.toString();
  }
}
